package com.cnap.service.impl;

import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import com.cnap.db.entity.IndicatorPO;
import com.cnap.db.mapper.IndicatorMapper;
import com.cnap.model.indicator.IndicatorExportModel;
import com.cnap.model.indicator.IndicatorTypeEnum;
import com.cnap.model.indicator.QueryIndicatorParam;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <类说明>
 *
 * @author xc
 **/
@Component
public class IndicatorExportServer implements IExcelExportServer {

    private final IndicatorMapper indicatorMapper;

    @Autowired
    public IndicatorExportServer(IndicatorMapper indicatorMapper) {
        this.indicatorMapper = indicatorMapper;
    }

    /**
     * 查询
     *
     * @param o 参数
     * @param i 页码
     * @return 结果
     */
    @Override
    public List<Object> selectListForExcelExport(Object o, int i) {
        // 分页查询
        PageMethod.startPage(i, ((QueryIndicatorParam) o).getPageSize());
        List<IndicatorPO> indicatorPOS = indicatorMapper.selectIndicatorByParam((QueryIndicatorParam) o);
        if (Objects.isNull(indicatorPOS) || indicatorPOS.isEmpty()) {
            return Collections.emptyList();
        }
        return indicatorPOS.stream().map(this::toExportModel).collect(Collectors.toList());
    }

    /**
     * 转换成导出模型
     *
     * @return 结果
     */
    private IndicatorExportModel toExportModel(IndicatorPO indicatorPO) {
        IndicatorExportModel model = new IndicatorExportModel();
        model.setIndex(indicatorPO.getId());
        if (IndicatorTypeEnum.CAL.getValue().equals(indicatorPO.getIndicatorType())) {
            // 算力
            model.setIndicatorType("算力度量");
        } else if (IndicatorTypeEnum.NET.getValue().equals(indicatorPO.getIndicatorType())) {
            // 网络
            model.setIndicatorType("网络度量");
        }
        // 分类
        model.setCategory(indicatorPO.getCategory());
        // 类型
        model.setType(indicatorPO.getType());
        // key
        model.setKey(indicatorPO.getKey());
        // value
        model.setValue(indicatorPO.getValue());
        // unit
        model.setUnit(indicatorPO.getUnit());

        return model;
    }
}
